#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <iostream>

int main()
{
    //创建套接字
    int sockfd = ::socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd < 0)
    {
        printf("socket fail...");
        return -1;
    }

    //向server写消息（自动bind）
    struct sockaddr_in dst_addr;
    memset(&dst_addr,0,sizeof dst_addr);
    dst_addr.sin_family = AF_INET;
    dst_addr.sin_port = htons(8888);
    dst_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    socklen_t len = sizeof dst_addr;
    std::string outbuffer;
    getline(std::cin,outbuffer);
    int m = ::sendto(sockfd,(void*)outbuffer.c_str(),sizeof outbuffer,0,
    (struct sockaddr*)&dst_addr,len);

    if(m < 0)
    {
        printf("sendto fail...");
        return -2;
    }
    return 0;
}
